// main scene
var g_mainScene = null;
var MainScene = cc.Scene.extend({
    _levelMaps : null,
    _levelTowers : null,
    _levelEnemies : null,
    ctor:function () {
        this._super();
        g_mainScene=this;
        g_mainScene.retain();

        ClearBuffer();

        this._levelMaps = new Array();
        this._levelTowers = new Array();
        this._levelEnemies = new Array();

        var filename=JsonResource("maps");
        this.initMaps(filename);
        filename=JsonResource("tows");
        this.initTowers(filename);
        filename=JsonResource("enes");
        this.initEnemies(filename);

        this.loadArmature();
    },
    onEnter:function () {
        this._super();
        AddBackLayer(this);
        AddMainLayer(this);
        AddUiLayer(this);

        var filename = AudioResource("bmusic.mp3");
        cc.audioEngine.playMusic(filename,true);
    },
    onExit:function(){
        this._levelMaps.splice(0,this._levelMaps.length);
        this._levelTowers.splice(0,this._levelTowers.length);
        this._levelEnemies.splice(0,this._levelEnemies.length);
        this._super();
    },
    loadArmature : function(){
        ArmatureResourceLoad("res/main/bdtx/bdtx");
        ArmatureResourceLoad("res/main/ywtx/ywtx");
        ArmatureResourceLoad("res/main/xztx/xztx");
    },
    initMaps : function(configfilename){
        var configJson=CreateJsonWithFile(configfilename);
        if( configJson ){
            for ( var i=0;i<configJson.files.length;i++){
                var filename=configJson.files[i];
                var jsonObj=CreateJsonWithFile(filename);
                this._levelMaps.push(jsonObj);
            }
            cc.log("map,init down,length: "+this._levelMaps.length);
        }else{
            cc.log("map,json invalid,file name: "+configfilename);
        }
    },
    initTowers : function(configfilename){
        var configJson=CreateJsonWithFile(configfilename);
        if( configJson ){
            for ( var i=0;i<configJson.files.length;i++){
                var filename=configJson.files[i];
                var jsonObj=CreateJsonWithFile(filename);
                this._levelTowers.push(jsonObj);
            }
            cc.log("tower,init down,length: "+this._levelTowers.length);
        }else{
            cc.log("tower,json invalid,file name: "+configfilename);
        }
    },
    initEnemies : function(configfilename){
        var configJson=CreateJsonWithFile(configfilename);
        if( configJson ){
            for ( var i=0;i<configJson.files.length;i++){
                var filename=configJson.files[i];
                var jsonObj=CreateJsonWithFile(filename);
                this._levelEnemies.push(jsonObj);
            }
            cc.log("tower,init down,length: "+this._levelEnemies.length);
        }else{
            cc.log("tower,json invalid,file name: "+configfilename);
        }
    }
});
MaxLevel=function(){
    if(g_mainScene){
        var maxLevel = g_mainScene._levelMaps.length;
        cc.log("getting maps by level: "+maxLevel);
        return maxLevel
    }else{
        cc.log("g_mainScene is invalid");
    }
}
GetLevelMap=function(level){
    if(g_mainScene){
        cc.log("getting maps by level: "+level);
        return g_mainScene._levelMaps[level];
    }else{
        cc.log("g_mainScene is invalid");
    }
}
GetLevelTower=function(level){
    if(g_mainScene){
        cc.log("getting towers by level: "+level);
        return g_mainScene._levelTowers[level];
    }else{
        cc.log("g_mainScene is invalid");
    }
}
GetLevelEnemy=function(level){
    if(g_mainScene){
        cc.log("getting enemies by level: "+level);
        return g_mainScene._levelEnemies[level];
    }else{
        cc.log("g_mainScene is invalid");
    }
}